/*     */ package com.zimbra.qa.unittest.prov.ldap;
/*     */ 
/*     */ import com.unboundid.ldap.sdk.LDAPConnection;
/*     */ import com.unboundid.ldap.sdk.LDAPConnectionPool;
/*     */ import com.unboundid.ldap.sdk.LDAPConnectionPoolStatistics;
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.util.CliUtil;
/*     */ import com.zimbra.cs.ldap.LdapClient;
/*     */ import com.zimbra.cs.ldap.LdapUsage;
/*     */ import com.zimbra.cs.ldap.ZAttributes;
/*     */ import com.zimbra.cs.ldap.unboundid.UBIDLdapContext;
/*     */ import com.zimbra.qa.unittest.prov.LocalconfigTestUtil;
/*     */ import java.io.File;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.io.OutputStream;
/*     */ import java.io.PrintStream;
/*     */ import java.net.Socket;
/*     */ import java.util.regex.Matcher;
/*     */ import java.util.regex.Pattern;
/*     */ import org.junit.AfterClass;
/*     */ import org.junit.Assert;
/*     */ import org.junit.BeforeClass;
/*     */ import org.junit.Test;
/*     */ import org.newsclub.net.unix.AFUNIXServerSocket;
/*     */ import org.newsclub.net.unix.AFUNIXSocket;
/*     */ import org.newsclub.net.unix.AFUNIXSocketAddress;
/*     */ import org.newsclub.net.unix.AFUNIXSocketException;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestLdapConnectivity
/*     */ {
/*     */   static
/*     */   {
/*  58 */     boolean assertsEnabled = false;
/*  59 */     assert ((assertsEnabled = 1) != 0);
/*     */     
/*  61 */     if (assertsEnabled) {
/*  62 */       throw new RuntimeException("Asserts must NOT be enabled!!!");
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private static enum ConnectionConfig
/*     */   {
/* 130 */     LDAP("ldap://localhost:389", "ldap://localhost:389", "0", "false", "false"), 
/*     */     
/* 132 */     LDAPI("ldapi://", "ldapi://", "0", "false", "false"), 
/*     */     
/* 134 */     LDAPS_T_UNTRUSTED_T_MISMATCHED("ldaps://localhost:636", "ldaps://localhost:636", "0", "true", "true"), 
/*     */     
/*     */ 
/* 137 */     LDAPS_T_UNTRUSTED_F_MISMATCHED("ldaps://localhost:636", "ldaps://localhost:636", "0", "true", "false"), 
/*     */     
/*     */ 
/* 140 */     LDAPS_F_UNTRUSTED_T_MISMATCHED("ldaps://localhost:636", "ldaps://localhost:636", "0", "false", "true"), 
/*     */     
/*     */ 
/* 143 */     LDAPS_F_UNTRUSTED_F_MISMATCHED("ldaps://localhost:636", "ldaps://localhost:636", "0", "false", "false"), 
/*     */     
/*     */ 
/* 146 */     STARTTLS_T_UNTRUSTED_T_MISMATCHED("ldap://localhost:389", "ldap://localhost:389", "1", "true", "true"), 
/*     */     
/*     */ 
/* 149 */     STARTTLS_T_UNTRUSTED_F_MISMATCHED("ldap://localhost:389", "ldap://localhost:389", "1", "true", "false"), 
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 158 */     STARTTLS_F_UNTRUSTED_T_MISMATCHED("ldap://localhost:389", "ldap://localhost:389", "1", "false", "true"), 
/*     */     
/*     */ 
/* 161 */     STARTTLS_F_UNTRUSTED_F_MISMATCHED("ldap://localhost:389", "ldap://localhost:389", "1", "false", "false");
/*     */     
/*     */ 
/*     */ 
/*     */     private String ldap_url;
/*     */     
/*     */     private String ldap_master_url;
/*     */     
/*     */     private String ldap_starttls_supported;
/*     */     
/*     */     private String ssl_allow_untrusted_certs;
/*     */     
/*     */     private String ssl_allow_mismatched_certs;
/*     */     
/*     */ 
/*     */     private ConnectionConfig(String ldap_url, String ldap_master_url, String ldap_starttls_supported, String ssl_allow_untrusted_certs, String ssl_allow_mismatched_certs)
/*     */     {
/* 178 */       this.ldap_url = ldap_url;
/* 179 */       this.ldap_master_url = ldap_master_url;
/* 180 */       this.ldap_starttls_supported = ldap_starttls_supported;
/* 181 */       this.ssl_allow_untrusted_certs = ssl_allow_untrusted_certs;
/* 182 */       this.ssl_allow_mismatched_certs = ssl_allow_mismatched_certs;
/*     */     }
/*     */     
/*     */     void setLocalConfig() throws Exception {
/* 186 */       LocalconfigTestUtil.modifyLocalConfigTransient(LC.ldap_url, this.ldap_url);
/* 187 */       LocalconfigTestUtil.modifyLocalConfigTransient(LC.ldap_master_url, this.ldap_master_url);
/* 188 */       LocalconfigTestUtil.modifyLocalConfigTransient(LC.ldap_starttls_supported, this.ldap_starttls_supported);
/* 189 */       LocalconfigTestUtil.modifyLocalConfigTransient(LC.ssl_allow_untrusted_certs, this.ssl_allow_untrusted_certs);
/* 190 */       LocalconfigTestUtil.modifyLocalConfigTransient(LC.ssl_allow_mismatched_certs, this.ssl_allow_mismatched_certs);
/* 191 */       LC.reload();
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   @BeforeClass
/*     */   public static void init()
/*     */     throws Exception
/*     */   {
/* 203 */     System.setProperty("javax.net.ssl.trustStore", LC.mailboxd_truststore.value());
/* 204 */     System.setProperty("javax.net.ssl.trustStorePassword", LC.mailboxd_truststore_password.value());
/*     */     
/* 206 */     CliUtil.toolSetup();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private UBIDLdapContext getContext()
/*     */     throws Exception
/*     */   {
/* 219 */     return (UBIDLdapContext)LdapClient.getContext(LdapUsage.UNITTEST);
/*     */   }
/*     */   
/*     */   private void closeContext(UBIDLdapContext zlc) {
/* 223 */     LdapClient.closeContext(zlc);
/*     */   }
/*     */   
/*     */   private void dumpUBIDSDKOject(String desc, Object obj)
/*     */   {
/* 228 */     System.out.println("\n--- " + desc);
/*     */     
/* 230 */     Pattern sPattern = Pattern.compile("([^(]*)\\((.*)");
/* 231 */     String asString = obj.toString();
/*     */     
/* 233 */     System.out.println(asString);
/*     */     
/* 235 */     Matcher matcher = sPattern.matcher(asString);
/* 236 */     if (matcher.matches()) {
/* 237 */       String className = matcher.group(1);
/* 238 */       String fields = matcher.group(2);
/*     */       
/* 240 */       if (fields.charAt(fields.length() - 1) == ')') {
/* 241 */         fields = fields.substring(0, fields.length() - 1);
/*     */       }
/*     */       
/* 244 */       String[] fieldArray = fields.split(",");
/*     */       
/* 246 */       System.out.println(className);
/* 247 */       for (String var : fieldArray) {
/* 248 */         System.out.println("  " + var.trim());
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private void dumpConnPool(LDAPConnectionPool connPool) {
/* 254 */     dumpUBIDSDKOject("connPool", connPool);
/*     */     
/* 256 */     LDAPConnectionPoolStatistics poolStats = connPool.getConnectionPoolStatistics();
/* 257 */     dumpUBIDSDKOject("poolStats", poolStats);
/*     */   }
/*     */   
/*     */ 
/*     */   public void testConnectivity(ConnectionConfig connConfig)
/*     */     throws Exception
/*     */   {
/* 264 */     connConfig.setLocalConfig();
/*     */     try
/*     */     {
/* 267 */       LdapClient.initialize();
/*     */       
/*     */       int expectedPort;
/*     */       int expectedPort;
/* 271 */       if (connConfig == ConnectionConfig.LDAPI) {
/* 272 */         expectedPort = 1; } else { int expectedPort;
/* 273 */         if ((connConfig == ConnectionConfig.LDAP) || (connConfig == ConnectionConfig.STARTTLS_T_UNTRUSTED_T_MISMATCHED) || (connConfig == ConnectionConfig.STARTTLS_T_UNTRUSTED_F_MISMATCHED) || (connConfig == ConnectionConfig.STARTTLS_F_UNTRUSTED_T_MISMATCHED) || (connConfig == ConnectionConfig.STARTTLS_F_UNTRUSTED_F_MISMATCHED))
/*     */         {
/*     */ 
/*     */ 
/*     */ 
/* 278 */           expectedPort = 389;
/*     */         } else {
/* 280 */           expectedPort = 636;
/*     */         }
/*     */       }
/* 283 */       UBIDLdapContext zlc1 = getContext();
/* 284 */       Assert.assertEquals(expectedPort, zlc1.getNative().getConnectedPort());
/*     */       
/* 286 */       ZAttributes attrs = zlc1.getAttributes("cn=zimbra", null);
/* 287 */       Assert.assertEquals("Zimbra Systems Application Data", attrs.getAttrString("description"));
/*     */       
/* 289 */       UBIDLdapContext zlc2 = getContext();
/* 290 */       Assert.assertEquals(expectedPort, zlc2.getNative().getConnectedPort());
/*     */       
/* 292 */       closeContext(zlc1);
/* 293 */       closeContext(zlc2);
/*     */     }
/*     */     finally {
/* 296 */       LdapClient.shutdown();
/*     */     }
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void LDAP() throws Exception {
/* 302 */     testConnectivity(ConnectionConfig.LDAP);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void LDAPI() throws Exception {
/* 307 */     testConnectivity(ConnectionConfig.LDAPI);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void LDAPS_T_UNTRUSTED_T_MISMATCHED() throws Exception {
/* 312 */     testConnectivity(ConnectionConfig.LDAPS_T_UNTRUSTED_T_MISMATCHED);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void LDAPS_T_UNTRUSTED_F_MISMATCHED() throws Exception {
/* 317 */     testConnectivity(ConnectionConfig.LDAPS_T_UNTRUSTED_F_MISMATCHED);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void LDAPS_F_UNTRUSTED_T_MISMATCHED() throws Exception {
/* 322 */     testConnectivity(ConnectionConfig.LDAPS_F_UNTRUSTED_T_MISMATCHED);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void LDAPS_F_UNTRUSTED_F_MISMATCHED() throws Exception {
/* 327 */     testConnectivity(ConnectionConfig.LDAPS_F_UNTRUSTED_F_MISMATCHED);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void STARTTLS_T_UNTRUSTED_T_MISMATCHED() throws Exception {
/* 332 */     testConnectivity(ConnectionConfig.STARTTLS_T_UNTRUSTED_T_MISMATCHED);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void STARTTLS_T_UNTRUSTED_F_MISMATCHED() throws Exception {
/* 337 */     testConnectivity(ConnectionConfig.STARTTLS_T_UNTRUSTED_F_MISMATCHED);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void STARTTLS_F_UNTRUSTED_T_MISMATCHED() throws Exception {
/* 342 */     testConnectivity(ConnectionConfig.STARTTLS_F_UNTRUSTED_T_MISMATCHED);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void STARTTLS_F_UNTRUSTED_F_MISMATCHED() throws Exception {
/* 347 */     testConnectivity(ConnectionConfig.STARTTLS_F_UNTRUSTED_F_MISMATCHED);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   @AfterClass
/*     */   public static void cleanup()
/*     */     throws Exception
/*     */   {}
/*     */   
/*     */ 
/*     */ 
/*     */   public static class SimpleTestServer
/*     */   {
/*     */     public static void main(String[] args)
/*     */       throws IOException
/*     */     {
/* 364 */       File socketFile = new File(new File(System.getProperty("java.io.tmpdir")), "junixsocket-test.sock");
/*     */       
/*     */ 
/* 367 */       AFUNIXServerSocket server = AFUNIXServerSocket.newInstance();
/* 368 */       server.bind(new AFUNIXSocketAddress(socketFile));
/* 369 */       System.out.println("server: " + server);
/*     */       
/* 371 */       while (!Thread.interrupted()) {
/* 372 */         System.out.println("Waiting for connection...");
/* 373 */         Socket sock = server.accept();
/* 374 */         System.out.println("Connected: " + sock);
/*     */         
/* 376 */         InputStream is = sock.getInputStream();
/* 377 */         OutputStream os = sock.getOutputStream();
/*     */         
/* 379 */         System.out.println("Saying hello to client " + os);
/* 380 */         os.write("Hello, dear Client".getBytes());
/* 381 */         os.flush();
/*     */         
/* 383 */         byte[] buf = new byte[''];
/* 384 */         int read = is.read(buf);
/* 385 */         System.out.println("Client's response: " + new String(buf, 0, read));
/*     */         
/*     */ 
/* 388 */         os.close();
/* 389 */         is.close();
/*     */         
/* 391 */         sock.close();
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static class SimpleTestClient
/*     */   {
/*     */     public static void main(String[] args)
/*     */       throws IOException
/*     */     {
/* 406 */       File socketFile = new File(new File(System.getProperty("java.io.tmpdir")), "junixsocket-test.sock");
/*     */       
/*     */ 
/* 409 */       AFUNIXSocket sock = AFUNIXSocket.newInstance();
/*     */       try {
/* 411 */         sock.connect(new AFUNIXSocketAddress(socketFile));
/*     */       } catch (AFUNIXSocketException e) {
/* 413 */         System.out.println("Cannot connect to server. Have you started it?");
/* 414 */         System.out.flush();
/* 415 */         throw e;
/*     */       }
/* 417 */       System.out.println("Connected");
/*     */       
/* 419 */       InputStream is = sock.getInputStream();
/* 420 */       OutputStream os = sock.getOutputStream();
/*     */       
/* 422 */       byte[] buf = new byte[''];
/*     */       
/* 424 */       int read = is.read(buf);
/* 425 */       System.out.println("Server says: " + new String(buf, 0, read));
/*     */       
/* 427 */       System.out.println("Replying to server...");
/* 428 */       os.write("Hello Server".getBytes());
/* 429 */       os.flush();
/*     */       
/* 431 */       os.close();
/* 432 */       is.close();
/*     */       
/* 434 */       sock.close();
/*     */       
/* 436 */       System.out.println("End of communication.");
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/prov/ldap/TestLdapConnectivity.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */